from django.shortcuts import render

# Create your views here.
import json
from django.contrib.auth import login, authenticate
from django.views import View
from django.http import JsonResponse, HttpResponseRedirect
from django.contrib.auth import logout
from wangziye.models import User



class LoginView(View):

    def post(self, request):
        '''实现登录接口'''
        # 1.接收参数
        dict = json.loads(request.body.decode())
        mobile = dict.get('mobile')
        password = dict.get('password')

        # 2.校验(整体 + 单个)
        if not all([mobile, password]):
            return JsonResponse({'errno': '4103',
                                 'errmsg': '缺少必传参数'})

        try:

            # account 是手机号
            # 根据手机号从数据库获取 user 对象返回.
            user = User.objects.get(mobile=mobile)

            # 3.验证是否能够登录
            user = authenticate(username=user.username,
                                password=password)

            # 判断是否为空,如果为空,返回
            if user is None:
                return JsonResponse({'errno': '4001',
                                     'errmsg': '用户名或者密码错误'})
        except Exception as e:
            return JsonResponse({
                'errno': '4001',
                'errmsg': '数据库错误'
            })

        # 4.状态保持
        login(request, user)

        return JsonResponse({'errno': '0',
                             'errmsg': '登录成功'})

    def get(self, request):
        # 获取界面需要的数据,进行拼接
        # info = {
        # 'username': request.user.username,
        # username = request.user.username
        #     'email': request.user.email,
        #     'email_active': request.user.email_active
        # }
        if request.user.is_authenticated:
        # 返回响应
            return JsonResponse({
                "errno": '0',
                "errmsg": "已登录",
                "data": {
                    "name": request.user.username,
                    "user_id": request.user.id
                }})
        else:
            return JsonResponse({
                "errno": '4101',
                "errmsg": "未登录",
                })

    # 判断用户是否登录
    # def get(self,request):
    #     if request.user.is_authenticated:
    #         return JsonResponse({
    #                         "errno": "0",
    #                         "errmsg": "已登录",
    #                         "data": {
    #                         "name": "用户名"
    #                                 }
    #                             })
    #     else:
    #         return JsonResponse({
    #                 "errno": "4101",
    #                 "errmsg": "未登录"
    #                         })

    # 定义退出登录的接口
    def delete(self, request):
        """实现退出登录逻辑"""

        # 清理 session
        logout(request)

        # 创建 response 对象.
        response = JsonResponse({'errno': 0,
                                 'errmsg': '已登出'})

        # 调用对象的 delete_cookie 方法, 清除cookie
        response.delete_cookie('mobile')

        # 返回响应
        return response

